<?php
namespace App\Http\Controllers\Home\Index;

use App\Http\Controllers\Controller;
use App\Http\Model\Article;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Tags;
use App\Http\Model\Link;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    //首页标签列表
    public function tag()
    {
        $keyword = request('keyboard');
        $list = Tags::select()->get()->toArray();
        $res = Link::select()->get()->toArray();
        $result = Article::select()->paginate(10);
        if (!empty($keyword)){
            $result = Article::where('article_name', 'like', "%{$keyword}%")->paginate(2);
        }
        foreach ($result as $k=>$v){
            $time = $v['add_time'];
            $result[$k]['add_time'] = date('Y-m-d',strtotime($time));
          if ($v['article_category'] == 1){
              $result[$k]['article_category'] = '网站首页';
          }
          if ($v['article_category'] == 2){
              $result[$k]['article_category'] = 'PHP';
          }
          if ($v['article_category'] == 3){
              $result[$k]['article_category'] = 'SQL';
          }
          if ($v['article_category'] == 4){
              $result[$k]['article_category'] = '关于我';
          }
      }
        return view('Home/Index/index')->with(['list'=>$list,'res'=>$res,'result'=>$result]);
    }

     // 搜索
    public function searchs(){
        $keyword = request('keyboard');
        if (!empty($keyword)){
            $ret = Article::where('article_name', 'like', "%{$keyword}%")->get()->toArray();
        }
        foreach ($ret as $k=>$v){
            $time = $v['add_time'];
            $ret[$k]['add_time'] = date('Y-m-d',strtotime($time));
            if ($v['article_category'] == 1){
                $ret[$k]['article_category'] = '网站首页';
            }
            if ($v['article_category'] == 2){
                $ret[$k]['article_category'] = 'PHP';
            }
            if ($v['article_category'] == 3){
                $ret[$k]['article_category'] = 'SQL';
            }
            if ($v['article_category'] == 4){
                $ret[$k]['article_category'] = '关于我';
            }
        }
        return view('Home/Index/index');

    }







//    public function bb()
//    {
//        $data = DB::table('book')
//            ->join('category', 'book.category', '=', 'category.cate_id')
//            ->select('book.*', 'category.cate_name')->orderBy('add_time','desc');
//        return $data;
//    }
//    public function Index()
//    {
//        if (empty(session('user_info'))){
//            $nickname = '';
//        }else{
//            $nickname = session('user_info')['nickname'];
//        }
//        //最新图书
//        $new = $this->bb()->where('type','1')->skip(0)->take(10)->get();
//        //登录下方的四个
//        $arr = DB::table('set')->get();
//        //书单广场
//        $datas = $this->bb()->where('type','2')->skip(0)->take(9)->get();
//        //精品
//        $quality = $this->bb()->whereIn('type',['3','4','5'])->get();
//        //热门
//        $host = $this->bb()->where('type','6')->skip(0)->take(24)->get();
//        //随便看看
//        $casual = $this->bb()->where('type','7')->skip(0)->take(24)->get();
//        return view('Home/Index/index')->with(['data'=>$new,'arr'=>$arr,'datas'=>$datas,'quality'=>$quality,'host'=>$host,'casual'=>$casual,'nickname'=>$nickname]);
//    }


}